﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Text;
using OgilvyOne.Ext.Weibo.Common;
using OgilvyOne.Ext.Weibo.Configuration;
using OgilvyOne.Ext.Weibo.Lib;

using ECCIT;
using ECCIT.Data;
using OgilvyOne.Model;
using OgilvyOne.BLL;
using OgilvyOne.Ext.Weibo.Model;

using Environment_v2 = OgilvyOne.Ext.Weibo.Configuration.Environment;

using Microblog_v2 = OgilvyOne.Ext.Weibo.Lib.Microblog;
using UserInfo_v2 = OgilvyOne.Ext.Weibo.Model.UserInfo;
using Users_v2 = OgilvyOne.Ext.Weibo.Model.Users;
using MicroblogException_v2 = OgilvyOne.Ext.Weibo.Common.MicroblogException;
using Source_v2 = OgilvyOne.Ext.Weibo.Model.Source;
using System.Xml.Serialization;

public partial class login : System.Web.UI.Page
{
    StringBuilder sb = new StringBuilder();
    protected void Page_Load(object sender, EventArgs e)
    {
        string accessToken = Request.QueryString["access_token"];
        string uid = Request.QueryString["uid"];
        string op = Request.QueryString["op"];
        try
        {

            if (!string.IsNullOrEmpty(op))
            {
                if (op == "islogin") //验证是否登录
                {
                    Response.ContentType = "text/xml";
                    Response.ContentEncoding = Encoding.UTF8;
                    sb.AppendLine(@"<?xml version='1.0' encoding='utf-8' ?>");
                    sb.AppendLine(@"<root>");

                    if (Session["weibo"] != null)
                    {
                        Weiboinfo wb = new Weiboinfo();
                        wb = (Session["weibo"] as Weiboinfo);
                        GetUserInfo(wb.WeiboToken, wb.WeiboUID);  //生成xml
                    }
                    else
                    {
                        sb.AppendLine(@"<result>0</result>");
                    }
                    sb.AppendLine(@"</root>");
                }
                if (op == "0")  //ajax：登录
                {
                    if (!string.IsNullOrEmpty(accessToken) && !string.IsNullOrEmpty(uid))
                    {
                        SetSession(accessToken, uid);
                        sb = new StringBuilder("1"); //成功
                    }
                    else
                    {
                        sb = new StringBuilder("0");  //失败
                    }
                }
                if (op == "getSession")
                {
                    if (Session["weibo"] != null)
                        sb = new StringBuilder("1"); //成功
                    else
                        sb = new StringBuilder("0"); //失败
                }
            }
        }
        catch(Exception ex)
        {
            sb = new StringBuilder("");
            Response.ContentType = "text/xml";
            Response.ContentEncoding = Encoding.UTF8;
            sb.AppendLine(@"<?xml version='1.0' encoding='utf-8' ?>");
            sb.AppendLine(@"<root>");
            sb.AppendLine(string.Format("<error>{0}</error>", ex.Message)); 
            sb.AppendLine(@"</root>");
        }
        Response.Write(sb);
        Response.End();
    }
    #region 将token和uid 写入Session["weibo"] Method： SetSession() 将guid写入Session["guid"]
    public void SetSession(string accessToken, string uid)
    {

        if (string.IsNullOrEmpty(accessToken)) throw new ArgumentNullException("access_token");
        OAuthAccessToken Otoken = new OAuthAccessToken();
        Otoken.Token = accessToken;

        Weiboinfo weiboInfo = new Weiboinfo();
        weiboInfo.WeiboToken = Otoken;
        weiboInfo.WeiboUID = uid;
        Session["weibo"] = weiboInfo;
        string guid = Guid.NewGuid().ToString();
        Session["guid"] = guid;
    }
    #endregion
    #region 根据token和uid 获取用户信息并存入表中 Method: GetUserInfo(OAuthAccessToken token,string uid)
    public void GetUserInfo(OAuthAccessToken token, string uid)
    {
        Int64 IntUid;
        Int64.TryParse(uid, out IntUid);
        UserInfo user = Microblog_v2.GetUserInfo(token, IntUid);
        /* <guid></guid>
         * <result></result>
          <userinfo>   
          <id></id>
          <screenname></screenname>
          <address></address>
          <headerimg></headerimg>
          </userinfo>
        */

        sb.AppendLine(string.Format("<guid>{0}</guid>", Session["guid"].ToString()));
        sb.AppendLine(@"<result>1</result>");
        sb.AppendLine(@"<userinfo>");
        sb.AppendLine(string.Format("<id>{0}</id>", user.ID.ToString()));
        sb.AppendLine(string.Format("<screenname><![CDATA[{0}]]></screenname>", user.ScreenName));
        sb.AppendLine(string.Format("<address><![CDATA[{0}]]></address>", user.Location));
        sb.AppendLine(string.Format("<headerimg>{0}</headerimg>", user.AvatarLarge));
        sb.AppendLine(@"</userinfo>");
    }
    #endregion
}
